﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrucksDir.Data;

namespace TrucksDir.Data.Services
{
    /// <summary>
    /// This is the data access layer management class.
    /// </summary>
    public partial class Context
    {
        private static readonly string UOW_INSTANCE_KEY = "trucksdirEntities_Instance";
        private static readonly object s_objSync = new object();
        private readonly trucksmodel m_context;

        private Context()
        {
            m_context = new trucksmodel();
        }

        public trucksmodel MyContext
        {
            get { return m_context; }
        }

        public static trucksmodel Instance
        {
            get
            {
                object instance = UnitOfWorkStore.GetData(UOW_INSTANCE_KEY);

                // Dirty, non-thread safe check
                if (instance == null)
                {
                    lock (s_objSync)
                    {
                        // Thread-safe check, now that we're locked
                        if (instance == null) // Ignore resharper warning that "expression is always true".  It's not considering thread-safety.
                        {
                            // Create a new instance of the MyDataLayer management class, and store it in the UnitOfWorkStore,
                            // using the string literal key defined in this class.
                            instance = new trucksmodel();
                            UnitOfWorkStore.SetData(UOW_INSTANCE_KEY, instance);
                        }
                    }
                }
                return (trucksmodel)instance;
            }        
            private set
            {
                UnitOfWorkStore.SetData(UOW_INSTANCE_KEY, value);
            }
        }

        public static void Dispose()
        {
            Instance = null;
        }

    }

}
